package service;

import mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountService {

    @Autowired
    AccountMapper account;

    /**
     * 转账的方法
     * @param srcId   转出的账户id
     * @param destId  转入的账户id
     * @param money   转账的金额
     */
    @Transactional
    public void transferAccounts(int srcId,int destId,int money){
        //从原账户中转出金额
        int srcCount = account.updateAccount(srcId,false,money);
        //向目标账户中转入金额
        int destCount = account.updateAccount(destId,true,money);

        //如果转入或转出的任何一方出现问题，手动抛出异常，会由事务进行处理，进行回滚操作
        if(srcCount!=1 || destCount!=1){
            throw new RuntimeException("转账失败");
        }

    }

}
